import axios from "axios";
import sessionTool from "./sessionTool";

axios.defaults.baseURL = process.env.VUE_APP_API_URL;

axios.interceptors.response.use((resp) => {
    return resp.data;
})

axios.interceptors.request.use(req => {

    let loginUser = sessionTool.getLoginUser();
    if (loginUser) {
        req.headers.Authorization = loginUser.id;
    }
    return req;
})

export default {
    post(url, params) {
        return axios({
            url,
            method: "POST",
            data: params
        })
    },
    get(url, params) {
        return axios({
            url,
            method: "GET",
            params
        })
    },
    upload(url, params) {
        return axios.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });
    }
}